package com.qingjiang.projectschedule.controller;

import com.qingjiang.projectschedule.pojo.Department;
import com.qingjiang.projectschedule.service.DepartmentService;
import com.qingjiang.projectschedule.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: yyg
 * @date: 2024/3/22
 */
@RestController
@RequestMapping("department")
@CrossOrigin
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 获取所有未弃用部门列表
     *
     * @return
     */
    @GetMapping("list")
    public Result getDeptList() {
        List<Department> deptList = departmentService.getDeptList();
        if (deptList == null) {
            return Result.build(null, 500, "获取部门列表失败", false);
        }
        return Result.ok(deptList);
    }

    /**
     * 获取全部部门列表，包括已弃用的
     *
     * @return
     */
    @GetMapping("getAllDeptList")
    public Result getAllDeptList() {
        return departmentService.getAllDeptList();
    }

    @PostMapping("updateDept")
    public Result updateDept(@RequestBody Department department) {
        return departmentService.updateDept(department);
    }

    @PostMapping("insertDept")
    public Result insertDept(@RequestBody Department department) {
        return departmentService.insertDept(department);
    }

    @PostMapping("getDepartments")
    public Result getDepartments(@RequestBody List<Integer> deptIDs) {
        List<Department> departments = departmentService.getDeptByID(deptIDs);
        return Result.ok(departments);
    }

    @GetMapping("getDepartmentsByType")
    public Result getDepartmentsByType(@Param("type") String type) {
        return departmentService.getDepartmentsByType(type);
    }
}
